package net.mingsoft.config;

import org.springframework.scheduling.annotation.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.scheduling.concurrent.*;
import java.util.concurrent.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAsync
public class AsyncConfig
{
    @Value("${ms.thread.core-pool-size:10}")
    private int corePoolSize;
    @Value("${ms.thread.max-pool-size:50}")
    private int maxPoolSize;
    @Value("${ms.thread.queue-capacity:1000}")
    private int queueCapacity;
    @Value("${ms.thread.keep-alive-seconds:300}")
    private int KeepAliveSeconds;
    @Value("${ms.thread.thread-name-prefix:lh-something-}")
    private String threadNamePrefix;
    @Value("${ms.thread.await-termination-seconds:60}")
    private int awaitTerminationSeconds;
    
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(this.corePoolSize);
        executor.setMaxPoolSize(this.maxPoolSize);
        executor.setQueueCapacity(this.queueCapacity);
        executor.setKeepAliveSeconds(this.KeepAliveSeconds);
        executor.setThreadNamePrefix(this.threadNamePrefix);
        executor.setAllowCoreThreadTimeOut(true);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(this.awaitTerminationSeconds);
        executor.setRejectedExecutionHandler((RejectedExecutionHandler)new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
